//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AmericanCondition.h"
using namespace Cephei::QL::Methods::Finitedifferences;
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::CAmericanCondition::CAmericanCondition (Cephei::QL::Math::IArray^ intrinsicValues) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CArray^ _CintrinsicValues;
    try
    {
#ifdef HANDLE
        _phAmericanCondition = NULL;
#endif
        _CintrinsicValues = safe_cast<CArray^> (intrinsicValues);
        _CintrinsicValues->Lock();
        QuantLib::Array& _intrinsicValues = static_cast<QuantLib::Array&> (_CintrinsicValues->GetReference ()); 
        _ppAmericanCondition = new boost::shared_ptr<QuantLib::AmericanCondition> (new QuantLib::AmericanCondition ( _intrinsicValues ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CintrinsicValues != nullptr) _CintrinsicValues->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::CAmericanCondition::CAmericanCondition (QL::Option::TypeEnum type, Double strike) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phAmericanCondition = NULL;
#endif
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //d
        _ppAmericanCondition = new boost::shared_ptr<QuantLib::AmericanCondition> (new QuantLib::AmericanCondition ( _type,  _strike ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::CAmericanCondition::CAmericanCondition (boost::shared_ptr<QuantLib::AmericanCondition>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanCondition = NULL;
#endif
	_ppAmericanCondition = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::CAmericanCondition::CAmericanCondition (QuantLib::AmericanCondition& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAmericanCondition = NULL;
#endif
	_ppAmericanCondition = new boost::shared_ptr<QuantLib::AmericanCondition> (&childNative);
    
    _AmericanConditionOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::CAmericanCondition::CAmericanCondition (CAmericanCondition^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanCondition = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAmericanCondition = new boost::shared_ptr<QuantLib::AmericanCondition> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::CAmericanCondition::CAmericanCondition (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanCondition = NULL;
#endif
	if (!t->IsSubclassOf(CAmericanCondition::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::CAmericanCondition::CAmericanCondition (QuantLib::Handle<QuantLib::AmericanCondition>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAmericanCondition = &childNative;
	_ppAmericanCondition = &static_cast<boost::shared_ptr<QuantLib::AmericanCondition>>(childNative.currentLink());
    
    _AmericanConditionOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::CAmericanCondition::CAmericanCondition (QuantLib::Handle<QuantLib::AmericanCondition> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAmericanCondition = &childNative;
	_ppAmericanCondition = &static_cast<boost::shared_ptr<QuantLib::AmericanCondition>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::CAmericanCondition::CAmericanCondition (QuantLib::AmericanCondition childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanCondition = NULL;
#endif
	_ppAmericanCondition = new boost::shared_ptr<QuantLib::AmericanCondition> (new QuantLib::AmericanCondition (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::CAmericanCondition::~CAmericanCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAmericanCondition != NULL)
    {
	    delete _ppAmericanCondition;
        _ppAmericanCondition = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::CAmericanCondition::!CAmericanCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAmericanCondition != NULL)
    {
	    delete _ppAmericanCondition;
    }
}
QuantLib::AmericanCondition& Cephei::QL::Methods::Finitedifferences::CAmericanCondition::GetReference ()
{
    if (_ppAmericanCondition == NULL) throw REFNEW NativeNullException ();
	return **_ppAmericanCondition;
}
boost::shared_ptr<QuantLib::AmericanCondition>& Cephei::QL::Methods::Finitedifferences::CAmericanCondition::GetShared ()
{
    if (_ppAmericanCondition == NULL) throw REFNEW NativeNullException ();
	return *_ppAmericanCondition;
}
QuantLib::AmericanCondition* Cephei::QL::Methods::Finitedifferences::CAmericanCondition::GetPointer ()
{
    if (_ppAmericanCondition == NULL) throw REFNEW NativeNullException ();
	return &**_ppAmericanCondition;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AmericanCondition>& Cephei::QL::Methods::Finitedifferences::CAmericanCondition::GetHandle ()
{
	if (_phAmericanCondition == NULL)
	{
		_phAmericanCondition = new Handle<QuantLib::AmericanCondition> (*_ppAmericanCondition);
	}
	return *_phAmericanCondition;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::CAmericanCondition::HasNative () 
{
	return (_ppAmericanCondition != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::IAmericanCondition^ Cephei::QL::Methods::Finitedifferences::CAmericanCondition_Factory::Create (Cephei::QL::Math::IArray^ intrinsicValues)
{
    return REFNEW CAmericanCondition ( intrinsicValues);
}
Cephei::QL::Methods::Finitedifferences::IAmericanCondition^ Cephei::QL::Methods::Finitedifferences::CAmericanCondition_Factory::Create (QL::Option::TypeEnum type, Double strike)
{
    return REFNEW CAmericanCondition ( type,  strike);
}
